package com.xilihui.xlh.core.http;

import android.support.v4.app.FragmentActivity;


import com.xilihui.xlh.business.dialogs.LoadingFragment;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.util.LogUtil;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class DoTransform {

    public static <T extends BaseEntity> Observable.Transformer<T, T> applyScheduler(final FragmentActivity activity, boolean showLoading) {
        return showLoading ? DoTransform.<T>applyLoading(activity) : DoTransform.<T>applyScheduler();
    }

    public static <T extends BaseEntity> Observable.Transformer<T, T> applyScheduler() {
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public static <T extends BaseEntity> Observable.Transformer<T, T> applyLoading(final FragmentActivity activity) {

        final LoadingFragment[] loadingFragment = new LoadingFragment[1];
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnSubscribe(new Action0() {

                            @Override
                            public void call() {
                                // start loading
                                LogUtil.i("mylogy","doOnSubscribe:call");
                                loadingFragment[0] = new LoadingFragment();
                                activity.getSupportFragmentManager().beginTransaction()
                                        .add(loadingFragment[0], "")
                                        .commitAllowingStateLoss();
                            }
                        }).doOnCompleted(new Action0() {
                            @Override
                            public void call() {
                                // stop loading
                                LogUtil.i("mylogy","doOnCompleted");
                                activity.getSupportFragmentManager()
                                        .beginTransaction()
                                        .remove(loadingFragment[0])
                                        .commitAllowingStateLoss();
                                loadingFragment[0] = null;
                            }
                        })
                        .doOnError(new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                // stop loading
                                LogUtil.i("mylogy","doOnError");
                                activity.getSupportFragmentManager()
                                        .beginTransaction()
                                        .remove(loadingFragment[0])
                                        .commitAllowingStateLoss();
                                loadingFragment[0] = null;
                            }
                        });
            }
        };
    }
}